/*
 * Copyright (C) 2012-2012 Teluu Inc. (http://www.teluu.com)
 * Contributed by Emre Tufekci (github.com/emretufekci)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#import "wrapper.h"
#import "CustomPJSUA2.hpp"

/**
 Create a object from .hpp class & wrapper to be able to use it via Swift
 */
@implementation CPPWrapper
PJSua2 pjsua2;



//Lib
/**
 Create Lib with EpConfig
 */
-(void) createLibWrapper
{
    return pjsua2.createLib();
};

/**
 Delete l
 */
-(void) deleteLibWrapper {
    pjsua2.deleteLib();
}

-(void) setLogLevel{
    
}

//Account
/**
 Create Account via following config(string username, string password, string ip, string port)
 */
-(void) createAccountWrapper :(NSString*) usernameNS :(NSString*) passwordNS :(NSString*) ipNS :(NSString*) portNS
{
    std::string username = std::string([[usernameNS componentsSeparatedByString:@"*"][0] UTF8String]);
    std::string password = std::string([[passwordNS componentsSeparatedByString:@"*"][0] UTF8String]);
    std::string ip = std::string([[ipNS componentsSeparatedByString:@"*"][0] UTF8String]);
    std::string port = std::string([[portNS componentsSeparatedByString:@"*"][0] UTF8String]);
    pjsua2.createAccount(username, password, ip, port);
}

/**
 Unregister account
 */
-(void) unregisterAccountWrapper {
    return pjsua2.unregisterAccount();
}

//Register State Info
/**
 Get register state true / false
 */
-(bool) registerStateInfoWrapper {
    return pjsua2.registerStateInfo();
}

// Factory method to create NSString from C++ string
/**
 Get caller id for incoming call, checks account currently registered (ai.regIsActive)
 */
- (NSString*) incomingCallInfoWrapper {
    NSString* result = [NSString stringWithUTF8String:pjsua2.incomingCallInfo().c_str()];
    return result;
}

/**
 Listener (When we have incoming call, this function pointer will notify swift.)
 */
- (void)incoming_call_wrapper: (void(*)())function {
    pjsua2.incoming_call(function);
}

/**
 Listener (When we have changes on the call state, this function pointer will notify swift.)
 */
- (void)call_listener_wrapper: (void(*)(int))function {
    pjsua2.call_listener(function);
}
//设置日志输出回调函数指针
-(void)log_writer_wrapper:(void(*)(int ,const char *,int))function{
    pjsua2.log_writer(function);
}

-(void) restoreCall{
 pjsua2.restoreAudioConnection();
}

-(void) pauseCall{
 pjsua2.pauseAudio();
}

-(void)im_receiver_wrapper:(void(*)(const char *))function{
 pjsua2.setIMReceiver(function);
}
-(void)callmedia_state_wrapper:(void(*)(int))function{
 pjsua2.setCallMediaState(function);
}

//现在第一次外呼，静音后打不开，后面的外呼就正常了？？ 还要研究啊
-(void)setMicOn:(BOOL)isOn{
    pjsua2.setMicOn(isOn);
}

-(void)setSpeakerOn:(BOOL)isOn{
 pjsua2.setSpeakerOn(isOn);
}

//拨号回调
-(void)dialNum:(NSString *)num{
    std::string cppnum = std::string([num UTF8String]);
    pjsua2.dialNum(cppnum);
}

/**
 Answer incoming call
 */
- (void) answerCallWrapper {
    pjsua2.answerCall();
}

/**
 Hangup active call (Incoming/Outgoing/Active)
 */
- (void) hangupCallWrapper {
    pjsua2.hangupCall();
}

/**
 Hold the call
 */
- (void) holdCallWrapper{
    pjsua2.holdCall();
}

/**
 unhold the call
 */
- (void) unholdCallWrapper{
    pjsua2.unholdCall();
}

/**
 Make outgoing call (string dest_uri) -> e.g. makeCall(sip:<SIP_USERNAME@SIP_IP:SIP_PORT>)
 2023-04-10 新增外呼参数
 */
-(void) outgoingCallWrapper :(NSString*) dest_uriNS :(NSDictionary *)param
{
    std::string dest_uri = std::string([[dest_uriNS componentsSeparatedByString:@"*"][0] UTF8String]);
    std::map<std::string,std::string> map;
    if(param != nil){
        for (int i=0;i<param.allKeys.count;i++){
            NSString * temp = param.allKeys[i];
            std::string  key = std::string([temp UTF8String]);
            std::string value = std::string([param[temp] UTF8String]);
            map[key] = value;
        }
    }
    pjsua2.outgoingCall(dest_uri,map);
}

-(void)sendOptions{
 pjsua2.sendOptions();
}

-(void)sendMessage:(NSString *)msg{
 std::string stdStr = [msg UTF8String];
 const char *cStr = stdStr.c_str();
 pjsua2.sendMessage(cStr);
}

-(void) outgoingServiceCallWrapper:(NSString *)dest_uriNS withIp:(NSString *)ipNS andPort:(NSString *)portNS andPhoneNum:(NSString *)phonenum andParams:(NSDictionary *)param{
    std::string dest_uri = std::string([[dest_uriNS componentsSeparatedByString:@"*"][0] UTF8String]);
    std::string ip = std::string([ipNS UTF8String]);
    std::string port = std::string([ipNS UTF8String]);
    std::string phone = std::string([phonenum UTF8String]);
    std::map<std::string,std::string> map;
   if(param != nil){
     for (int i=0;i<param.allKeys.count;i++){
         NSString * temp = param.allKeys[i];
         std::string  key = std::string([temp UTF8String]);
         std::string value = std::string([param[temp] UTF8String]);
         map[key] = value;
      }
    }
    pjsua2.callServiceNum(dest_uri,ip,port,phone,map);
}

-(void)destroy{
    pjsua2.destroy();
}
-(void)reinit{
    pjsua2.reinit();
}

@end

